#!/usr/bin/env python

import os, sys
from concurrency import ExecutionContext
from concurrency.parser import parse
import optparse

class OptionParser (optparse.OptionParser):

    def check_required (self, opt):
        option = self.get_option(opt)
        # Assumes the option's 'default' is set to None!
        if getattr(self.values, option.dest) is None:
            self.error("%s option not supplied" % option)

def main(options, args):

    fp = getattr(options, 'filename')
    fp = os.path.abspath(os.path.expanduser(fp))

    infile = open(fp, 'rb')
    history = parse(infile)
    print 'Read history:', history
    ec = ExecutionContext(history=history)
    print 'Executing history...'
    list(ec)
    print 'Execution finished.'
    print 'Final history:', ec.history

def parse_cmd():
    parser = OptionParser()
    parser.add_option('-f', '--file',
                      dest='filename',
                      help=('File containing a execution history'
                            'or a set of transactions to be analyzed'),
                      metavar='FILE')
    (options, args) = parser.parse_args()
    parser.check_required('-f')
    main(options, args)

if __name__ == '__main__':
    parse_cmd()
